package com.yxdj.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yxdj.common.core.utils.DateUtils;
import com.yxdj.user.api.domain.Address;
import com.yxdj.user.api.domain.User;
import com.yxdj.user.mapper.AddressMapper;
import com.yxdj.user.mapper.RegionMapper;
import com.yxdj.user.mapper.UserMapper;
import com.yxdj.user.service.IUserService;
import io.jsonwebtoken.lang.Collections;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户信息Service业务层处理
 *
 * @author mty
 * @date 2025-03-18
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService
{
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private AddressMapper addressMapper;

    @Autowired
    private RegionMapper regionMapper;

    /**
     * 查询用户信息
     *
     * @param id 用户信息主键
     * @return 用户信息
     */
    @Override
    public User selectUserById(Long id)
    {
        User user = userMapper.selectUserById(id);
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Address> addressLambdaQueryWrapper = new LambdaQueryWrapper<>();
        //封装查询条件
        addressLambdaQueryWrapper.eq(Address::getUserId,id);
        //带条件的查询
        List<Address> addressList = addressMapper.selectList(addressLambdaQueryWrapper);
        if(!Collections.isEmpty(addressList)){
            //遍历所有的地址
            for (Address address : addressList) {
                //设置省、市、区的名称
                address.setProvinceName(regionMapper.getReginonName(address.getProvinceId()));
                address.setCityName(regionMapper.getReginonName(address.getCityId()));
                address.setDistrictName(regionMapper.getReginonName(address.getDistrictId()));
            }
            //将用户地址设置到用户对象中
            user.setAddressList(addressList);
        }
        return user;
    }

    /**
     * 查询用户信息列表
     *
     * @param user 用户信息
     * @return 用户信息
     */
    @Override
    public List<User> selectUserList(User user)
    {
        return userMapper.selectUserList(user);
    }

    /**
     * 新增用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int insertUser(User user)
    {
        user.setCreateTime(DateUtils.getNowDate());
        return userMapper.insertUser(user);
    }

    /**
     * 修改用户信息
     *
     * @param user 用户信息
     * @return 结果
     */
    @Override
    public int updateUser(User user)
    {
        user.setUpdateTime(DateUtils.getNowDate());
        return userMapper.updateUser(user);
    }

    /**
     * 批量删除用户信息
     *
     * @param ids 需要删除的用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserByIds(Long[] ids)
    {
        return userMapper.deleteUserByIds(ids);
    }

    /**
     * 删除用户信息信息
     *
     * @param id 用户信息主键
     * @return 结果
     */
    @Override
    public int deleteUserById(Long id)
    {
        return userMapper.deleteUserById(id);
    }
}
